<%--
  Created by IntelliJ IDEA.
  User: CURRY
  Date: 2021/10/13
  Time: 19:40
  To change this template use File | Settings | File Templates.
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" href="<%=basePath%>static/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="<%=basePath%>static/css/font.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/xadmin.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap.css">
    <script type="text/javascript" src="<%=basePath%>static/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/jqPaginator.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/xadmin.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/pintuer.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/admin.css">

</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">体检人员列表</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <span class="x-right" style="line-height:40px" id="showTotal"></span>
    <input type="hidden" id="doctorName" name="doctorName" value="${username}"/></div>
<table class="layui-table">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>医生</th>
        <th>症状</th>
        <th>状态</th>
        <th>日期</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tab"></tbody>
    <tr>
        <td colspan="9" align="center">
            <ul class="pagination" id="pagination1"></ul>
        </td>
    </tr>
</table>
</div>
<script type="text/javascript">
    // 总页数
    var totalPages = 0;
    // 总条数
    var totalCounts = 0;
    // 当前页
    var currentPage = 0;
    // 页面加载完成就去执行此代码，主要目的就是加载按钮，以及当页面中pageNum发生变化时，去执行后台查询数据方法
    $(document).ready(function () {
        // 第一次执行selectAllUser函数，用于初始化参数
        selectAllUser(-1);
        // 加载按钮
        $("#pagination1").jqPaginator({
            // 总页数
            totalPages: totalPages,
            // 总条数
            totalCounts: totalCounts,
            // 当前页
            currentPage: currentPage,
            // 首页
            first: '<li class="first"><a href="javascript:void(0)">首页</a></li>',
            // 上一页
            prev: '<li class="prev"><a href="javascript:void(0)">上一页</a></li>',
            // 下一页
            next: '<li class="next"><a href="javascript:void(0)">下一页</a></li>',
            // 末页
            last: '<li class="last"><a href="javascript:void(0)">末页</a></li>',
            // 当前页
            page: '<li class="page"><a href="javascript:void(0)">{{page}}</a></li>',
            // pageNum改变执行函数，参数n其实就是pageNum
            onPageChange: function (n) {
                selectAllUser(n);
            }
        })
    })

    // selectAllUser
    function selectAllUser(n) {
        // ajax请求
        $.ajax({
            // 请求服务器的路径
            url: "../doctor/selectAllMaser",
            // 提交方式
            type: "post",
            // 服务器响应数据类型
            dataType: "json",
            // 此需求需要设置为同步请求
            async: false,
            // 发送给服务器的数据内容
            data: {
                "pageNum": n,
                "pageSize": 10,
                "doctorName": $("#doctorName").val(),
            },//成功回调函数
            success: function (data) {
                // 显示数据条数
                $("#showTotal").text("共有数据：" + data.total + "条");
                // 获取用户数据
                let userList = data.list;
                if (userList.length > 0) {
                    // 当数据库有用户数据的时候，进行初始化
                    if (n === -1) {
                        totalPages = data.pages;
                        totalCounts = data.total;
                        currentPage = data.pageNum;
                    }
                    // 定义一个空字符串，用来拼接用户数据信息
                    let str = '';
                    // 通过一个for循环，拼接用户数据
                    for (let i = 0; i < userList.length; i++) {
                        str += "<tr>" +
                            "<td>" + userList[i].appointId + "</td>" +
                            "<td>" + userList[i].name + "</td>" +
                            "<td>" + userList[i].age + "</td>" +
                            "<td>" + userList[i].sex + "</td>" +
                            "<td>" + userList[i].doctor + "</td>" +
                            "<td>" + userList[i].symptom + "</td>" +
                            "<td>" + userList[i].state + "</td>" +
                            "<td>" + userList[i].appointmentDate + "</td>" +
                            "<td>" +
                            '<div class="button-group"><button type="button" class="button border-main" href="/jsp/reply.jsp"><span\n' +
                            '                            class="icon-edit" ></span> 回馈</button><button class="button border-red"' +
                            '                        onclick="del(' + "'" + userList[i].appointId + "'" + ')"><span\n' +
                            '                    class="icon-trash-o"></span> 删除</button></div>' +
                            "</td>" +
                            "</tr>";
                    }
                    // 把拼接好的用户数据信息字符串str，通过jQuery方法html()，填充在页面tbody中
                    $("#tab").html(str);
                } else {
                    // 当数据库没有用户数据的时候，进行初始化
                    totalPages = 1;
                    totalCounts = 1;
                    currentPage = 1;
                    $("#tab").html("<tr><td colspan='8' align='center'>没有用户数据，请添加</td></tr>")
                }
            },// 失败回调
            error: function () {
                alert("服务器错误");
            }
        })
    }

    //delOne
    function del(id) {
        $.ajax({
            url: "../doctor/delOne",
            type: "post",
            dataType: "json",
            data: {
                "appointId": id,
            },
            success:function (data) {

            },
            error:function () {
                if (confirm("您确定要删除吗?") == false) {
                    return false;
                } else {
                    return true;
                }

            }
        })
    }

    //reply
    function reply(id) {
        $.ajax({
            url: "../doctor/reply",
            type: "post",
            dataType: "json",
            data: {
                "appointId": id,
            }
        })
    }
</script>
</body>
</html>